package guinong.com.gnw.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RetryPolicy;
import com.google.gson.Gson;

import java.util.HashMap;

import guinong.com.gnw.R;
import guinong.com.gnw.api.AppHttpApi;
import guinong.com.gnw.core.AppApiFactory;

import guinong.com.gnw.event.EventBusSendFactory;
import guinong.com.gnw.utils.SharedPreferencesUtils;
import okhttp3.RequestBody;
import rx.subscriptions.CompositeSubscription;

/**
 * @author chenshuangniu
 * @time 2016/4/14 15:33
 * @修改内容 这是一个基类  配置mvp中的p的参数
 * @版本
 */
public abstract class BasePresenter<Iview extends BaseIView> {
    public static final AppHttpApi mGN  = AppApiFactory.getApiInstance();//api
    protected Iview mView;//view接口
    protected Activity mContext;
    protected Gson mGson;
    protected HashMap<String, String> mGetParams;//get请求参数
    protected HashMap<String, String> mPostParams;//post参数
    protected HashMap<String, RequestBody> MpostFileParams;//post file参数
    protected Resources mRes;//资源数据
    protected String[] userInfo;//用户登录数据
    protected String[] userLoginInfo;//用户信息
    protected SharedPreferencesUtils mShareDataUtils;
    protected CompositeSubscription mRxManager = new CompositeSubscription();
    public BasePresenter(Activity context, Iview view) {
        mContext = context;
        mView = view;
        mGson = new Gson();
        mShareDataUtils = SharedPreferencesUtils.getInstance(mContext);
        mRes = context.getResources();
    }

    /**
     * used to check the network is available or not
     *
     * @return
     */
    public boolean checkNetwork() {
        ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return false;
                    }
                }
            }
        }
        toastShortBasic(mRes.getString(R.string.toast_no_network));
        return true;
    }

    public void toastShortBasic(String msg) {
        EventBusSendFactory.newPostThread(msg);
    }


    public void onDestroy() {
        mRxManager.unsubscribe();
    }
    /**超时时间**/
    public static RetryPolicy retryPolicy(){
        return new DefaultRetryPolicy(20 * 1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }
}
